1034. 边界着色
https://leetcode-cn.com/problems/coloring-a-border/
- 提交时间:2021-12-07 15:56:22
- 执行用时:16 ms, 在所有 Go 提交中击败了70.37%的用户
- 内存消耗:6.6 MB, 在所有 Go 提交中击败了25.93%的用户
- 通过测试用例:154 / 154
var dirs = []struct{ x, y int }{
{1, 0},
{0, 1},
{-1, 0},
{0, -1},
}
func colorBorder(grid [][]int, row int, col int, color int) [][]int {
m, n := len(grid), len(grid[0])
type point struct{ x, y int }
//边界点
borders := []point{}
originalColor := grid[row][col]
//领地
vis := make([][]bool, m)
for i := range vis {
vis[i] = make([]bool, n)
}
var dfs func(x, y int)
dfs = func(x, y int) {
//遍历四个方向
isBorder := false
for _, dir := range dirs {
nx := x + dir.x
ny := y + dir.y
if !(0 <= nx && nx < m && 0 <= ny && ny < n && grid[nx][ny] == originalColor) {
isBorder = true
} else if !vis[nx][ny] {
vis[nx][ny] = true
dfs(nx, ny)
}
// if dirx == m || dirx < 0 || diry == n || diry < 0 {
// isBorder = true
// continue
// }
// fmt.Printf("vis:%+v,dirx:%+v,diry:%+v\n", vis, dirx, diry)
// //领地
// if vis[dirx][diry] {
// continue
// }
// if grid[dirx][diry] != originalColor {
// //边界
// isBorder = true
// dfs(dirx, diry)
// continue
// } else {
// //新领地
// vis[dirx][diry] = true
// dfs(dirx, diry)
// }
}
if isBorder {
borders = append(borders, point{x, y})
}
}
dfs(row, col)
// fmt.Printf("borders:%+v\n", borders)
for _, point := range borders {
grid[point.x][point.y] = color
}
return grid
}